package com.kobeliu.controller;

import com.kobeliu.service.ShortLinkService;
import com.kobeliu.util.CommonUtil;
import com.kobeliu.vo.ShortLinkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LinkApiController {

    @Autowired
    private ShortLinkService shortLinkService;

    @GetMapping(path = "{/shortLinkCode}")
    public void dispatch(@PathVariable(name = "shortLinkCode") String shortLinkCode,
                         HttpServletRequest request, HttpServletResponse response){
        if(isLetterDigit(shortLinkCode)){
            //查找短链 TODO
            ShortLinkVO shortLinkVO = shortLinkService.parseShortLinkCode(shortLinkCode);
            if(shortLinkVO!=null){


            }

        }


    }

//    private static boolean isVisitable(ShortLinkVO shortLinkVO){
//        if(shortLinkVO!=null && shortLinkVO.getExpired().getTime()> CommonUtil.getCurrentTimestamp()){
//            if(ShortLinkState)
//        }
//    }

    /**
     * 仅包括数字和字母
     * @param str
     * @return
     */
    private static boolean isLetterDigit(String str){
        String regex = "^[a-z0-9A-Z]+$";
        return str.matches(regex);
    }

}
